/*

Frequency of use: 1 2 [3] 4 5 MEDIUM

Short:
    A Prototype is an object which is cloneable, i.e. you can create a copy, 
even though you don't know what you are creating a copy of.

Long:
    The prototype pattern is a creational design pattern used in software 
development when the type of objects to create is determined by a prototypical 
instance, which is cloned to produce new objects. This pattern 
is used to:

 - avoid subclasses of an object creator in the client application, like the 
abstract factory pattern does.

 - avoid the inherent cost of creating a new object in the standard way (e.g., 
using the 'new' keyword) when it is prohibitively expensive for a given 
application.

To implement the pattern, declare an abstract base class that specifies a pure 
virtual clone() method. Any class that needs a "polymorphic constructor" 
capability derives itself from the abstract base class, and implements the 
clone() operation.
The client, instead of writing code that invokes the "new" operator on a 
hard-coded class name, calls the clone() method on the prototype, calls a 
factory method with a parameter designating the particular concrete derived 
class desired, or invokes the clone() method through some mechanism provided by 
another design pattern.

Information sources:
 - http://calumgrant.net/patterns/index.html
 - http://en.wikipedia.org/wiki/Prototype_pattern
 - http://www.dofactory.com/Patterns/Patterns.aspx
*/

#include <iostream>
#include <memory>

class DoSomething
{
public:
	virtual ~DoSomething() { }
	virtual std::auto_ptr<DoSomething> clone() const=0;
	virtual void do_it()=0;
};

class HelloWorld : public DoSomething
{
public:
	std::auto_ptr<DoSomething> clone() const
	{
		return std::auto_ptr<DoSomething>(new HelloWorld);
	}
	void do_it()
	{
		std::cout << "Hello world!" << std::endl;
	}
};

void hello_world(const DoSomething & something)
{
	std::auto_ptr<DoSomething> clone = something.clone();
	clone->do_it();
}

int main()
{
	hello_world(HelloWorld());
	return 0;
}

